package cn.desk.application.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.desk.application.entity.Application;
import cn.desk.commons.exception.DeskException;
import cn.desk.commons.service.impl.BaseServiceImpl;
import cn.desk.commons.util.QueryCondition;

public class ApplicationServiceImpl<T extends Application> extends
		BaseServiceImpl<T> {
	private static final Logger log = LoggerFactory
			.getLogger(ApplicationServiceImpl.class);

	public void add(T t) throws cn.desk.commons.exception.DeskException {
		log.debug("add start");
		QueryCondition condition = new QueryCondition();
		condition.andEq("name", t.getName());
		List<T> list = baseDao.list(Application.class, condition);
		if (list.size() != 0) {
			throw new DeskException("EM0001", t);
		}
		Integer id = getNextId(Application.class);
		t.setId(String.valueOf(id));
		baseDao.insert(t);
		log.debug("add end");
	}

	public cn.desk.commons.entity.PageMode<T> listPage(
			cn.desk.commons.entity.PageMode<T> page, T t) {
		QueryCondition condition = new QueryCondition();
		if (t.getId() != null && !t.getId().equals("")) {
			condition.andEq("id", t.getId());
		}
		if (t.getName() != null && !t.getName().equals("")) {
			condition.andLike("name", t.getName(),
					QueryCondition.MATCH_TYPE_ANY);
		}
		condition.desc("name");
		page = baseDao.listPage(page, Application.class, condition);
		return page;
	};
}
